<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;

class BatchController extends BaseController {

	/*
	 * 发送手机验证码
	 */
	public function verifynote(){
		$tel = I('post.usertel');
		$fun= new \Org\Util\fun();
		$tmppwd = substr(mt_rand()."000000",0,6);
		$ssmmess = $fun->SendSms($tel,$tmppwd);
		session('verftel',null);
		session('verftel',$tmppwd);
		//echo $tmppwd;
	}
	
	/*
	 * 获取session保存的验证码
	 */
	public function getverifynote(){
		$userpass = I('post.userpass');
		if(!empty(session('verftel'))){
			if($userpass==session('verftel')){
				echo 1; //通过
			}else{
				echo 0; //不正确
			}
		}else{
			echo 2;//验证码过期
		}
	}
	
	/*
	 * 真一物一码
	 * 手机验证通过保存
	 */
	public function savetel(){
		$tel = I('post.usertel');
		$randma = I('post.randma');
		$useropenid = I('post.useropenid');
		$telres = D(C('TABLE_TELINFO'));
		$num = $telres->where(array('tel'=>$tel))->count();
		if($num < 1){
			$data['ma_randinfo'] = $randma;
			$data['tel'] = $tel;
			$data['useropenid'] = $useropenid;
			$data['regdate'] = date("Y-m-d H:i:m");
			$res = $telres->add($data);
			if($res){
				echo 1;//保存成功
			}else{
				echo 2;//保存失败
			}
		}else{
			echo 3;//存在此号码
		}
	}

	/*
	 * 固定码
	 * 手机验证通过保存
	 */
	public function savetelfalse(){
		$tel = I('post.usertel');
		$rootid = I('post.rootid');
		$useropenid = I('post.useropenid');
		$telres = D(C('TABLE_TELINFO'));
		$res = $telres->where(array('ma_randinfo'=>$rootid,'useropenid'=>$useropenid,'tel'=>$tel))->find();
		if($res){
			//如果存在openid和手机号码则显示领取次数
			if($res['hbnum']!=1){
				echo 0;
			}else{
				echo 1;
			}
		}else{
			//保存openid和手机号码信息
			$data['ma_randinfo'] = $rootid;
			$data['tel'] = $tel;
			$data['useropenid'] = $useropenid;
			$data['regdate'] = date("Y-m-d H:i:m");
			$res = $telres->add($data);
			if($res){
				echo 'ok';//保存成功
			}else{
				echo -1;//保存失败
			}
		}
	}
	
	public function savetel1(){
		$telres = D(C('TABLE_TELINFO'));
		$tel = I('post.usertel');
		$num = $telres->where(array('tel'=>$tel))->find();
		if($num){
			echo $num['hbnum'];
		}else{
			echo 'no';//不存在
		}
	}

	//判断是否手机号是否被注册过
	public function isreginfo(){
		$telres = D(C('TABLE_TELINFO'));
		$tel = I('post.usertel');
		$num = $telres->where(array('tel'=>$tel))->count();
		if($num > 0){
			echo 1; //存在
		}else{
			echo 0; //不存在
		}
	}

	//保存注册信息
	public function addresreg(){
		$openid = $_COOKIE['user_userinfo'];
		$marootid = I('post.rootid');
		$usertel = I('post.usertel');
		$randcode = I('post.randcode');
		$username = I('post.username');
		$address = I('post.address');

		$checkinfo = $this->checkinfo($randcode,$openid);
		if(!$checkinfo){
			$telres = D(C('TABLE_TELINFO'));
			$data['marootid'] = $marootid;
			$data['ma_randinfo'] = $randcode;
			$data['useropenid'] = $openid;
			$data['tel'] = $usertel;
			$data['username'] = $username;
			$data['address'] = $address;
			$data['regdate'] = date("Y-m-d H:i:m");
			$res = $telres->add($data);
			if($res){
				echo $res;
			}else{
				echo 0;
			}
		}else{
			echo $checkinfo;
		}
	}

	//判断是否具有相同的信息
	protected function checkinfo($randcode,$openid){
		$telres = D(C('TABLE_TELINFO'));
		$res = $telres->where(array('ma_randinfo'=>$randcode,'useropenid'=>$openid))->find();
		if($res){
			return $res['id'];
		}else{
			return 0;
		}
	}
    /*
     * 真一物一码
     * 抢红包成功则设置该码的状态
     */
    public function setmastate(){
    	$randma = I('post.randma');
    	$total_amount = I('post.total_amount');
    	$openid = I('post.openid');
    	$mch_billno = I('post.mch_billno');
    	$status = I('post.status');
    	if($status==1){		/*如果红包发放成功，则返回1*/
    		$rand = D(C('TABLE_RANDNUMBER_HB'));
    		$num = $rand->where(array('ma_randnumber'=>$randma))->count();
    		if($num > 0){	/*开始判断是否已经被领取一次红包+2*/
    			$this->savehbdata($randma, $total_amount, $openid, $mch_billno, 2);
    		}else{			/*开始判断是否已经被领取一次红包+1*/
    			$this->savehbdata($randma, $total_amount, $openid, $mch_billno, 1);
    		}
    	}else{
    		echo 2; //参数错误
    	}
    }
    
    /*
     * 真一物一码
     * 检测红包码是否有效
     */
    public function checkhbma(){
    	$randma = I('post.randma');
    	$rand = D(C('TABLE_RANDNUMBER'));
    	$num = $rand->where(array('ma_randnumber'=>$randma))->count();
    	if($num>0){
    		echo 1;//存在
    	}else{
    		echo 0;//不存在
    	}
    }

	/*
     * 固定码
     * 检测红包码是否有效
     */
	public function checkhbmafasle(){
		$rootid = I('post.rootid');
		$type = D(C('TABLE_TYPE'));
		$num = $type->where(array('id'=>$rootid,'isvote'=>1))->count();
		if($num>0){
			echo 1;//存在
		}else{
			echo 0;//不存在
		}
	}
	public function setmastatefalse(){
		$rootid = I('post.rootid');
		$openid = I('post.openid');
		$status = I('post.status');
		if($status==1){
			$telres = D(C('TABLE_TELINFO'));
			$data['hbnum'] = 1;
			$res = $telres->where(array('ma_randinfo'=>$rootid,'useropenid'=>$openid))->save($data);
			if($res){
				echo 1;//更新成功
			}else{
				echo 0;//更新失败
			}
		}
	}

	protected function islogin($getURL){
		if(empty($_COOKIE['user_userinfo'])){
			header("Location:".C('LOGINADDRES').$getURL);
			exit;
		}
	}


	protected function savehbdata($randma, $total_amount, $openid, $mch_billno, $cishutoal){
    	$rand = D(C('TABLE_RANDNUMBER_HB'));
    	$data['ma_randnumber'] = $randma;
    	$data['ma_money'] = $total_amount;
    	$data['ma_money_state'] = 1;
    	$data['ma_user_openid'] = $openid;
    	$data['ma_mch_billno'] = $mch_billno;
    	$data['ma_randnumber_date'] = date("Y-m-d H:i:m");
    	$res = $rand->add($data);
    	if($res){
    		$this->setmahbcishu($randma,$cishutoal);
    		echo 1; //成功
    	}else{
    		echo 0; //失败
    	}
    }
    
    /*
     * 真一物一码
     * 获取红包领取次数
     */
    public function getcishu(){
    	$randma = I('post.randma');
    	if(!empty($randma)){
    		$rand = D(C('TABLE_RANDNUMBER'));
    		$res = $rand->where(array('ma_randnumber'=>$randma))->find();
    		if(!empty($res)){
    			echo $res['ma_hb_cishu'];
    		}else{
    			echo 0;
    		}
    	}else{
    		echo 0;
    	}
    }

	/*
     * 固定码
     * 获取红包领取次数
     */
	public function getcishufalse(){
		$rootid = I('post.rootid');
		$userinfo = I('post.userinfo');
		$telinfo = D(C('TABLE_TELINFO'));
		$res = $telinfo->where(array('ma_randinfo'=>$rootid,'useropenid'=>$userinfo))->find();
		if($res){
			if($res['hbnum']!=1){
				echo 0; //未领取
			}else{
				echo 1; //已领取
			}
		}else{
			echo -1; //没有此信息
		}
	}
    
    
    /*
     * 领取完跳转的页面
     */
    public function getpackets(){
    	$money = number_format(I('get.money')*0.01, 2);
    	$randma = I('get.getma');
    	$rand = D(C('TABLE_RANDNUMBER'));
    	$res = $rand->where(array('ma_randnumber'=>$randma))->find();
    	if($res){
    		$this->assign('mainfo',$res);
    		$this->assign('money',$money); //金额
    		$this->display('Index/getpackets');
    	}else{
    		echo "<script>alert('此码不存在！');</script>";
    	}
    }

	/*
	 * 改变此码的领取次数
	 */
	public function changema_cishu(){
		$this->islogin(getURL());
		$randcode = I('post.randcode');
		$userinfoid = I('post.userinfoid');
		$randma = D(C('TABLE_RANDNUMBER'));
		$data['ma_hb_cishu'] = 1;
		$data['iswhoget'] = $userinfoid;
		$data['zjtime'] = date("Y-m-d H:i:m");
		$res = $randma->where("ma_randnumber='$randcode' and ma_hb_cishu!=1")->save($data);
		if($res==0){
			echo 0;
		}else if($res>0){
			echo 1;
		}else{
			echo -1;
		}

	}

	//如果是红包码则插入用户的openid信息
	public function saveuseropenid(){
		$userinfo = $_COOKIE['user_userinfo'];
		$randcode = I('post.randcode');
		$randma = D(C('TABLE_RANDNUMBER'));
		$data['iswhoget'] = $userinfo;
		$res = $randma->where(array('ma_randnumber'=>$randcode))->save($data);
		if($res){
			$res = array('ret'=>1);
			$this->inserttjinfo($randcode);
		}else{
			$res = array('ret'=>0);
		}
		echo json_encode($res);
	}


	//添加访问者信息
	protected function inserttjinfo($maid){
		$userinfo = $_COOKIE['user_userinfo'];
		$tjinfo = iptoarea();
		$ip = $tjinfo['ip'];
		$statistics = D(C('TABLE_STATISTICS'));
		$count = $statistics->where(array('useropenid'=>$userinfo,'ip'=>$ip))->count();
		if($count==0){
			if(!empty($ip)){
				$data['useropenid'] = $userinfo;
				$data['maid'] = $maid;
				$data['country'] = $tjinfo['country'];
				$data['area'] = $tjinfo['area'];
				$data['region'] = $tjinfo['region'];
				$data['city'] = $tjinfo['city'];
				$data['county'] = $tjinfo['county'];
				$data['isp'] = $tjinfo['isp'];
				$data['ip'] = $ip;
				$data['visitdate'] = date("Y-m-d H:i:m");
				$res = $statistics->add($data);
				if($res){
					return true;
				}else{
					return false;
				}
			}
		}
	}

	//检查此码的奖品是否被人领取
	public function is_checkawardget(){
		$this->islogin(getURL());
		$randcode = I('post.randcode');
		$randma = D(C('TABLE_RANDNUMBER'));
		$res = $randma->where(array('ma_randnumber'=>$randcode))->find();
		if($res){
			$r = $res['ma_hb_cishu'];
		}else{
			$r = -1;
		}
		echo $r;
	}

	/*
	 * 根据抽奖获取的华人钻，添加至个人账户里
	 */
	public function adddrill(){
		$randcode = I('post.randcode');
		$diamond = I('post.diamond');
		$userinfo = $_COOKIE['user_userinfo'];
		$bodystr = I('post.bodystr');
		$r = request_get(C('APIADDRES1')."/addscore?token=".$userinfo."&score=".$diamond."&msg=".urlencode($bodystr));
		if($r['ret']==1){
			echo 1;	//成功
		}else{
			echo -1;
		}
	}

	/*
	 *查询此奖品是什么类型的，同时返回实物图片跟名称
	 */
	public function chaxun_award(){
		$this->islogin(getURL());
		$matype = I('post.matype');
		if($matype==1){
			$res = array('awardkind'=>1);
		}else{
			$award = D(C('TABLE_AWARD'));
			$res = $award->find($matype);
		}
		echo json_encode($res);

	}

}